package org.jeecg.modules.lida.service;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.leave.entity.AgLeave;
import org.json.XML;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.jeecg.modules.lida.entity.GroupPO;
import org.jeecg.modules.lida.entity.ItemPO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class LidaService {
    @Value("${lida.serverUrl}")
    String serverUrl = "";

    /*
     *根据人员编号获取人员信息
     */
    public String doGetPersonCardInfoByCode(String personnalCode) throws Exception {
        String AppKey="Administrator";
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyyMMddHHmmss");
        String TimeStamp= myFmt.format(new Date());
        String ValidCode="system" ;
        //创建工厂
        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        //Client client = factory.createClient("http://222.190.145.146:9009/webservice/AccessWS.asmx?wsdl");
        Client client = factory.createClient(serverUrl);
        Object[] objects = new Object[0];
        // invoke("方法名",参数1,参数2,参数3....);
        objects = client.invoke("GetPersonCardInfoByCode", AppKey, TimeStamp,personnalCode, ValidCode);
        String xml = objects[0].toString();
        org.json.JSONObject xmlJSONObj = XML.toJSONObject(xml);
        System.out.println(xmlJSONObj.get("Integration"));
        JSONObject jsonObject12 = (JSONObject) JSON.parse(xmlJSONObj.get("Integration").toString());
        JSONObject jsonObject2 = (JSONObject) jsonObject12.get("GetPersonCardInfoByCode");
        JSONObject jsonObject3 = (JSONObject) jsonObject2.get("Body");
        JSONObject jsonObject4 = (JSONObject) jsonObject3.get("Item");
        String personnalID= jsonObject4.get("PersonnalID").toString();
        System.out.println(personnalID);
        return personnalID;
    }

    /*
     *获取门禁权限组表
     */
    public List<GroupPO> doGetAccessSecurityGroups() throws Exception {
        String AppKey="Administrator";
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyyMMddHHmmss");
        String TimeStamp= myFmt.format(new Date());
        Integer SecurityGroupID=-1;
        String ValidCode="system" ;
        //创建工厂
        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        //Client client = factory.createClient("http://222.190.145.146:9009/webservice/AccessWS.asmx?wsdl");
        Client client = factory.createClient(serverUrl);
        Object[] objects = new Object[0];
        // invoke("方法名",参数1,参数2,参数3....);
        objects = client.invoke("GetAccessSecurityGroups", AppKey, TimeStamp, SecurityGroupID, ValidCode);
        String xml = objects[0].toString();
        org.json.JSONObject xmlJSONObj = XML.toJSONObject(xml);
        System.out.println(xmlJSONObj.get("Integration"));
        System.out.println();
        JSONObject jsonObject12 = (JSONObject) JSON.parse(xmlJSONObj.get("Integration").toString());
        JSONObject jsonObject2 = (JSONObject) jsonObject12.get("GetAccessSecurityGroups");
        JSONObject jsonObject3 = (JSONObject) jsonObject2.get("Body");
        String json3 = jsonObject3.toString();
        JSONObject jsonObject4 = (JSONObject) JSON.parse(json3);
        JSONArray jsonObject5 = (JSONArray) jsonObject4.get("Group");
        List<GroupPO> grops = JSONObject.parseArray(jsonObject5.toString(),GroupPO.class);
        List<ItemPO> pojos=null;
        for(int i=0;i<jsonObject5.size();i++){
            JSONObject jsonstr2 = (JSONObject) JSON.parse(jsonObject5.get(i).toString());
            try{
                JSONArray jsonObject6 = (JSONArray) jsonstr2.get("Item");
                pojos = JSONObject.parseArray(jsonObject6.toString(),ItemPO.class);
                grops.get(i).setPojoList(pojos);
            }catch (Exception e){
                List<ItemPO> pojos2=new ArrayList<>();
                JSONObject jsonObject7 = jsonObject5.getJSONObject(i);
                JSONObject jsonObject8 = jsonObject7.getJSONObject("Item");
                ItemPO itemPO = (ItemPO) net.sf.json.JSONObject.toBean(net.sf.json.JSONObject.fromObject(jsonObject8), ItemPO.class);
                pojos2.add(itemPO);
                grops.get(i).setPojoList(pojos2);
            }
        }
//        for (GroupPO grop :grops){
//            System.out.println(grop);
//        }
        return grops;
    }

    /**
     * 添加/删除人员门禁权限组
     */
    public List doOperatePersonSecurityGroupID(String PersonnelCode, List<GroupPO> SecurityGroupID) throws Exception {
        //String AppKey,String TimeStamp,String PersonnelCode,String ValidCode,int Type,List<Integer> SecurityGroupID
        String AppKey="Administrator";
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyyMMddHHmmss");
        String TimeStamp= myFmt.format(new Date());
        String ValidCode="system" ;

        //创建工厂
        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        Client client = factory.createClient(serverUrl);
        String str = "";
            Object[] objects = new Object[0];
            //根据人员编号获取人员信息
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("GetPersonCardInfoByCode", AppKey, TimeStamp, PersonnelCode, ValidCode);
        System.out.println("未解析数据:" + objects[0]);
        String xml = objects[0].toString();
        //转json
        org.json.JSONObject xmlJSONObj = XML.toJSONObject(xml);
        JSONObject jsonObject1 = (JSONObject) JSON.parse(xmlJSONObj.get("Integration").toString());
        JSONObject jsonObject2 = (JSONObject) jsonObject1.get("GetPersonCardInfoByCode");
        JSONObject jsonObject3 = (JSONObject) jsonObject2.get("Body");
        JSONObject jsonObject4 = (JSONObject) jsonObject3.get("Item");
        //PersonnalID
        Object PersonnalID = jsonObject4.get("PersonnalID");
        System.out.println(PersonnalID);
        //清空权限
        // AppKey,  TimeStamp,-1,ValidCode
        List<GroupPO> groupPOS = new LidaService().doGetAccessSecurityGroups();
        for (GroupPO po : groupPOS){
            String securityGroupID = po.getSecurityGroupID();
            int intsecurityGroupID = Integer.parseInt(securityGroupID);
            int intPersonnalID = Integer.parseInt(PersonnalID.toString());
            client.invoke("OperatePersonSecurityGroupID", AppKey, TimeStamp, 2, intPersonnalID, intsecurityGroupID, ValidCode);
        }
        //for循环  调用OperatePersonSecurityGroupID  添加/删除人员门禁权限组  Type 1：新增，2：删除
            List list = new ArrayList();
        if (SecurityGroupID==null){
            return null;
        }
            try {
                for (GroupPO po : SecurityGroupID) {
                Object[] result = new Object[0];
                String securityGroupID = po.getSecurityGroupID();
                int intsecurityGroupID = Integer.parseInt(securityGroupID);
                result = client.invoke("OperatePersonSecurityGroupID", AppKey, TimeStamp, 1, PersonnalID, intsecurityGroupID, ValidCode);
                xml = result[0].toString();
                //转json
                org.json.JSONObject xmlJSONObj2 = XML.toJSONObject(xml);
                str = xmlJSONObj2.toString();
                //转实体类
                ObjectMapper objectMapper = new ObjectMapper();
                Map map = objectMapper.readValue(str, Map.class);
                list.add(map.get("Integration").toString());
               }
                //返回
                return list;
//            for (Object liststr : list){
//                System.out.println(liststr);
//            }
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("数据处理异常");
            }
    }
}
